package com.bit.demo.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @PostMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
        //校验参数
        //验证密码
        //进行返回
//        if(userName == null || "".equals(userName) || password == null || "".equals(password)){
//
//        }
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //验证密码是否正确，理论来说，应该访问数据库
        //这里暂且先写死
        if("admin".equals(userName) && "admin".equals(password)){
            //登录成功，将用户信息放入session
            session.setAttribute("loginUserName",userName);
            return true;
        }
        return false;
    }
    @GetMapping("/getLoginUser")
    public String getLoginUser(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session == null) {
            return "";
        }
        return (String)session.getAttribute("loginUserName");
    }
}
